package cn.chenchao.eshop.user.action;

import cn.chenchao.eshop.common.action.BaseAction;
import cn.chenchao.eshop.common.entity.Message;
import cn.chenchao.eshop.log.dao.ISysLog;
import cn.chenchao.eshop.log.dao.imp.CreateLog;
import cn.chenchao.eshop.log.dao.imp.TextLog;
import cn.chenchao.eshop.user.entity.User;
import cn.chenchao.eshop.user.service.impl.UserServiceImpl;
import util.JsonUtil;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/22 15:49
 * @Author : Chen Chao
 * @File : eshop.cn.chenchao.eshop.user.action.UserAction.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 用户控制类
 * 处理所有用户后台操作，并返回json格式的字符串消息
 */
public class UserAction extends BaseAction {

    User user = new User();
    /**
     * 用户名
     */
    private String name;
    /**
     * 密码
     */
    private String password;

    /**
     * 登录功能
     *
     * @return 返回登录消息的json字符串
     */
    public String login() {
        /*1 封装User对象*/
        initAction();

        UserServiceImpl userService = new UserServiceImpl();
        try {
            /*2 调用UserService处理逻辑*/
            user = userService.userLogin(user);
            /*3 设置消息并返回*/
            if (user != null) {
                message.setMsgType(Message.SUCCESS);
                message.setMsg("登录成功！");
            } else {
                message.setMsgType(Message.FAILURE);
                message.setMsg("登录失败！");
            }
        } catch (Exception e) {
            /*4 处理异常*/
            e.printStackTrace();
            message.setMsg("服务器炸了");
            message.setMsgType(Message.FAILURE);
        }
        return JsonUtil.entity2Json(message);
    }

    /**
     * 注册功能
     */
    public void register() {
        /*1 封装User对象*/
        initAction();

        UserServiceImpl userService = new UserServiceImpl();
        try {
            /**/
            userService.userRegister(user);
            /*3.调用父类对象写入日志*/
            new TextLog().textLog(iSysLog.normal("注册成功"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void initAction(){
        user.setName(name);
        user.setPassword(password);
    }
    public UserAction() {
    }

    public UserAction(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
